<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no">
    <link rel="stylesheet" href="./css/common.css">

    <style>
        .item {
            height: 50px;
            border-bottom: 1px solid #ebebee;
        }
        
        nav {
            height: 50px;
        }
        
        .btn {
            width: 40%;
            height: 40px;
        }
        
        .btn.del {
            background-color: red;
        }
    </style>
</head>

<body>
    <nav class="bg-fff f20 aic flex pl-10">
        <a href="./list.html" style="width: 50px;">返回</a>
        <span class="fcc fg1">英雄详情</span>
        <span style="width: 50px;"></span>
    </nav>
    <ul class="mt-10" id="box">
        <!-- <li class="bg-fff item flex jc-sb pl-15 pr-15 aic f16 rel">
            <span class="f14">姓名</span>
            <span class="f14">张飞</span>
        </li>
        <li class="bg-fff item flex jc-sb pl-15 pr-15 aic f16 rel">
            <span class="f14">年龄</span>
            <span class="f14">29岁</span>
        </li>
        <li class="bg-fff item flex jc-sb pl-15 pr-15 aic f16 rel">
            <span class="f14">职业</span>
            <span class="f14">坦克</span>
        </li>
        <li class="bg-fff item flex jc-sb pl-15 pr-15 aic f16 rel">
            <span class="f14">技能</span>
            <span class="f14">技能1,技能2, 技能3</span>
        </li> -->
    </ul>

    <div class="fcc mt-30">
        <button class="btn edit"><a href="./edit.html" class="fff">编辑</a></button>
        <button onclick="obj.del();" class="del btn ml-15">删除</button>
    </div>


    <script src="https://cdn.bootcdn.net/ajax/libs/zepto/1.2.0/zepto.js"></script>
    <script>
        var search = location.search;
        var _id = search.replace(/^\?_id=(.*)/, '$1');
        var obj = {
            del: function() {
                $.ajax({
                    type: 'get',
                    url: 'http://localhost:8888/del',
                    data: {
                        _id: _id
                    },
                    dataType: 'json',
                    success: function(res) {
                        if (res.code === 666) {
                            alert('删除成功');
                            location.href = './list.html';
                        } else {
                            alert(res.msg);
                        }
                    },
                    error: function() {
                        // todo
                    }
                })
            },

            getDetail: function() {
                var self = this;
                $.ajax({
                    type: 'get',
                    url: 'http://localhost:8888/getDetail',
                    data: {
                        _id: _id
                    },
                    dataType: "json",
                    success: function(res) {
                        if (res.code === 666) {
                            self.render(res.data);
                        } else {
                            alert(res.msg);
                        }
                    },
                    error: function() {
                        // todo
                    }
                })
            },

            render: function(obj) {
                var str = ` <li class="bg-fff item flex jc-sb pl-15 pr-15 aic f16 rel">
                    <span class="f14">姓名</span>
                    <span class="f14">${obj.name}</span>
                </li>
                <li class="bg-fff item flex jc-sb pl-15 pr-15 aic f16 rel">
                    <span class="f14">年龄</span>
                    <span class="f14">${obj.age}岁</span>
                </li>
                <li class="bg-fff item flex jc-sb pl-15 pr-15 aic f16 rel">
                    <span class="f14">职业</span>
                    <span class="f14">坦克</span>
                </li>
                <li class="bg-fff item flex jc-sb pl-15 pr-15 aic f16 rel">
                    <span class="f14">技能</span>
                    <span class="f14">技能1,技能2, 技能3</span>
                </li>`;
                $('#box').html(str);
                $('.edit a').attr('href', `./edit.html?_id=${_id}`)

            }
        }
        obj.getDetail();
    </script>
</body>

</html>